package com.zofer.study.stack;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * 题目：实现一个特殊功能的栈，在实现栈的基本功能的基础上，再实现返回栈中最小元素的操作。
 */
public class GetMinStack {
    public int[] getMinStack (int[][] op) {
        // write code here
        Stack<Integer> dataStack = new Stack<Integer>();
        Stack<Integer> minStack = new Stack<Integer>();

        List<Integer> list = new ArrayList<Integer>();
        int len = op.length;
        for(int i = 0;i < len;i++){
            if(op[i][0] == 1){
                //入栈
                if(minStack.empty()){
                    minStack.push(op[i][1]);
                }else if(op[i][1] < minStack.peek()){
                    minStack.push(op[i][1]);
                }
                dataStack.push(op[i][1]);
            }else if(op[i][0] == 2){
                int tem = dataStack.pop();
                if(tem == minStack.peek()){
                    minStack.pop();
                }
            }else if(op[i][0] == 3){
                list.add(minStack.peek());
            }
        }

        int[] res = new int[list.size()];
        int index = 0;
        for(int i:list){
            res[index++] = i;
        }
        return res;
    }
}
